import Vue from "vue";

import { getToken } from '@/utils/storage'
import {Toast} from '@/utils/vant'

import VueRouter from "vue-router";
import Login from "@/views/login.vue"


import Find from '@/views/find'
import Layout from '@/views/layout'
import My from '@/views/my'
import Index from '@/views/index'
import Collect from '@/views/my/collect.vue'

import News from '@/views/news'
import Citylist from '@/views/citylist'
import Register from '@/views/register'
import Mytop from '@/views/my-top.vue'
// import Banner from '@/views/index-banner'
Vue.use(VueRouter);

const routes = [
  {
    path: '/', component:Layout ,
    redirct: '/index',
    children: [
      { path: '/index', component: Index },
      { path: '/find', component: Find },
      {
        path: '/my', component: My,
        children: [
          { path: '/mytop', component: Mytop },
        ]
      },
      { path: '/news', component: News },
      { path: '/login', component: Login },
    ]
  },
  { path: '/register', component: Register },
  { path: '/collect', component: Collect },
  { path: '/citylist', component: Citylist }


];

const router = new VueRouter({

  routes,
});
const whiteList = ['/login', '/register','/index','/my'] // 白名单列表，记录无需权限访问的所有页面

router.beforeEach((to, from, next) => {
  if (getToken()) {
    next()
  } else {
    // 没有token的人, 看看你要去哪
    // (1) 访问的是无需授权的页面（白名单），也是放行
    //     就是判断，访问的地址，是否在白名单数组中存在 includes
    if (whiteList.includes(to.path)) {
      next()
    } else {
      Toast.fail('请登录')
      next('/login')
    }
  }
})

export default router;
